﻿/*
 * CPPcheck for Visual Studio. An project to integrate
 * CPPcheck into visual studio as a native extension.
 * Copyright (C) 2012 Gordon Kristan
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using EnvDTE;
using System.Windows.Forms;

namespace GordonKristan.cppcheck
{
    /// <summary>
    [PackageRegistration(UseManagedResourcesOnly = true)]
    // Registry Key
    [DefaultRegistryRoot("Software\\cppcheckvs")]
	// Help/about screen information.
	[InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
    // PLK. Need to request from Microsoft when ready.
    [ProvideLoadKey("Standard", "1.0", "cppcheck", "Gordon Kristan", 1)]
    // This attribute is needed to let the shell know that this package exposes some menus.
    [ProvideMenuResource(1000, 1)]
    // This attribute registers a tool window exposed by this package.
    [ProvideToolWindow(typeof(ToolWindow))]
    [Guid(GuidList.guidcppcheckPkgString)]
	//Main package class for the extension
    public sealed class cppcheckPackage : Package
    {
		/// <summary>
		/// Default constructor of the package.
		/// Most Visual Studio services aren't loaded at this point,
		/// so it's best to do most of the work in the initializer.
		/// </summary>
        public cppcheckPackage()
        {
            
        }

        /// <summary>
        /// Initialization code.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // Add our command handlers for menu (commands must exist in the .vsct file)
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if ( null != mcs )
            {
				CommandID commandID;
				MenuCommand menuCommand; ;
                // Create the command for the check file option.
				commandID = new CommandID(GuidList.guidcppcheckCmdSet, (int)PkgCmdIDList.checkFileCommand);
				menuCommand = new MenuCommand(CheckFileCallback, commandID);
				mcs.AddCommand(menuCommand);
				// Create the command for the check project option.
				commandID = new CommandID(GuidList.guidcppcheckCmdSet, (int)PkgCmdIDList.checkProjectCommand);
				menuCommand = new MenuCommand(CheckProjectCallback, commandID);
				mcs.AddCommand(menuCommand);
				// Create the command for the configuration option.
				commandID = new CommandID(GuidList.guidcppcheckCmdSet, (int)PkgCmdIDList.configurationCommand);
				menuCommand = new MenuCommand(ConfigurationCallback, commandID);
				mcs.AddCommand(menuCommand);
                // Create the command for the tool window
				commandID = new CommandID(GuidList.guidcppcheckCmdSet, (int)PkgCmdIDList.toolWindow);
				menuCommand = new MenuCommand(ShowToolWindow, commandID);
				mcs.AddCommand(menuCommand);
            }
        }

		/// <summary>
		/// Called when the user chooses to show the tool window.
		/// </summary>
		private void ShowToolWindow(object sender, EventArgs e)
		{
			// Show the tool window. The last param creates it if it doesn't exits.
			ToolWindowPane window = this.FindToolWindow(typeof(ToolWindow), 0, true);
			if ((null == window) || (null == window.Frame))
			{
				throw new NotSupportedException(Resources.CanNotCreateWindow);
			}
			IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
			Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
		}

		/// <summary>
		/// Called when the check file menu option is selected.
		/// </summary>
		/// <param name="sender">Source of event.</param>
		/// <param name="e">Event details.</param>
		private void CheckFileCallback(object sender, EventArgs e)
		{
			ToolWindowPane window = this.FindToolWindow(typeof(ToolWindow), 0, true);
			EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
			String path = dte.ActiveDocument.Path;
			CheckModule module = new CheckModule(path, true);
			ListView.ListViewItemCollection items = module.getItems();
		}

		/// <summary>
		/// Called when the check project menu option is selected.
		/// </summary>
		/// <param name="sender">Source of event.</param>
		/// <param name="e">Event details.</param>
		private void CheckProjectCallback(object sender, EventArgs e)
		{
			ToolWindowPane window = this.FindToolWindow(typeof(ToolWindow), 0, true);
		}

		/// <summary>
		/// Called when the configuration menu option is selected.
		/// </summary>
		/// <param name="sender">Source of event.</param>
		/// <param name="e">Event details.</param>
		private void ConfigurationCallback(object sender, EventArgs e)
		{

		}
    }
}